package com.situ.mall.service.impl;

import java.util.List;

import javax.jws.soap.SOAPBinding.Use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.common.MD5Util;
import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.User;
import com.situ.mall.mapper.UserMapper;
import com.situ.mall.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;

	@Override
	public ServerResponse login(String username, String password) {
		//用户存不存在
		int count = userMapper.checkUserName(username);
		if (count == 0) {
			return ServerResponse.createError("用户不存在");
		}
		
		//走到这说明用户名存在
		User user = userMapper.selectLogin(MD5Util.EncodeUtf8(password),username);
		if (user == null) {
			return ServerResponse.createError("密码错误");
		}
		user.setPassword("");
		return ServerResponse.createSuccess("登陆成功", user);
	}

	@Override
	public ServerResponse pageList(Integer page, Integer limit,User user) {
		//分页插件
		PageHelper.startPage(page, limit);
		//查找数据
		List<User> list = userMapper.pageList(user);
		//封装pageInfo
		PageInfo pageInfo = new PageInfo<>(list);
		//查找总数据
		Integer count = (int)pageInfo.getTotal();
		//返回
		return ServerResponse.createSuccess("查找成功", count, list);
	}
	
	//Id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = userMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArray = ids.split(",");
		try {
			Integer count = userMapper.deleteAll(idArray);
			if (count == idArray.length) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}
	//添加
	@Override
	public ServerResponse insert(User user) {
		user.setPassword(MD5Util.EncodeUtf8(user.getPassword()));
		try {
			Integer count = userMapper.insert(user);
			if (count == 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
			
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	@Override
	public User toUpdate(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}
	//修改
	@Override
	public ServerResponse update(User user) {
		user.setPassword(MD5Util.EncodeUtf8(user.getPassword()));
		try {
			Integer count = userMapper.updateByPrimaryKeySelective(user);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

	//注册查重复
	@Override
	public ServerResponse checkName(String username) {
		try {
			Integer count = userMapper.checkUserName(username);
			if (count == 1) {
				return ServerResponse.createSuccess("用户名重复，请重新输入");
			} else {
				return ServerResponse.createError("用户名可以注册");
			}
			
		} catch (Exception e) {
			return ServerResponse.createError("未知错误");
		}
	}

	//注册信息加入数据库
	@Override
	public ServerResponse add(User user) {
		user.setPassword(MD5Util.EncodeUtf8(user.getPassword()));
		try {
			Integer count = userMapper.insert(user);
			if (count == 1) {
				return ServerResponse.createSuccess("注册成功");
			} else {
				return ServerResponse.createError("注册失败");
			}
			
		} catch (Exception e) {
			return ServerResponse.createError("注册失败");
		}
	}

	//后台列表界面展示每个订单的用户
	@Override
	public List<User> selectById(Integer userId) {
		return userMapper.selectById(userId);
	}

	
	
	
	
}
